package com.jrh.web.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jrh.biz.brand.BrandService;
import com.jrh.biz.img.ImgUploadService;
import com.jrh.biz.product.ProductService;
import com.jrh.biz.product.ref.ProductRefService;
import com.jrh.common.constant.JrhBizConstant;
import com.jrh.domain.entity.Brand;
import com.jrh.web.admin.form.AdminProductRefForm;

@Controller
@RequestMapping("/sales/pr/")
public class AdminProductRefController {

    @Autowired
    private ProductService    productService;

    @Autowired
    private ProductRefService productRefService;

    @Autowired
    private BrandService      brandService;

    @Autowired
    private ImgUploadService  imgUploadService;

    @RequestMapping("ad/list")
    public String adList(Integer pageIndex, HttpServletRequest request, HttpServletResponse response, Model model) {
        Brand brand = BrandSalesManager.getBrandByUser(brandService, request, response);
        if (brand == null) {
            return "/admin/error";
        }
        if (pageIndex == null) {
            pageIndex = 1;
        }
        model.addAttribute("brand", brand);
        model.addAttribute("pageData",
                           productRefService.getProductAdPage(brand.getId(), pageIndex, JrhBizConstant.SIZE_PER_PAGE));
        return "sales/product/ad_list";
    }

    @RequestMapping("group/list")
    public String groupList(Integer pageIndex, HttpServletRequest request, HttpServletResponse response, Model model) {
        Brand brand = BrandSalesManager.getBrandByUser(brandService, request, response);
        if (brand == null) {
            return "/admin/error";
        }
        if (pageIndex == null) {
            pageIndex = 1;
        }
        model.addAttribute("brand", brand);
        model.addAttribute("pageData", productRefService.getProductGroupPage(brand.getId(), pageIndex, JrhBizConstant.SIZE_PER_PAGE));
        return "sales/product/group_list";
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "group/delete/{id}")
    public String deleteProductGroup(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {
        try {
            Brand brand = BrandSalesManager.getBrandByUser(brandService, request, response);
            if (brand == null) {
                return Boolean.FALSE.toString();
            }
            productRefService.deleteProductGroupById(id);
            return String.valueOf(true);
        } catch (Exception e) {
            return Boolean.FALSE.toString();
        }
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "ad/delete/{id}")
    public String deleteProductAd(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {
        try {
            Brand brand = BrandSalesManager.getBrandByUser(brandService, request, response);
            if (brand == null) {
                return Boolean.FALSE.toString();
            }
            productRefService.deleteProductAdById(id);
            return String.valueOf(true);
        } catch (Exception e) {
            return Boolean.FALSE.toString();
        }
    }

    @RequestMapping(value = "group/create")
    public String createProductGroup(AdminProductRefForm productRef, HttpServletRequest request,
                                     HttpServletResponse response) {
        Brand brand = BrandSalesManager.getBrandByUser(brandService, request, response);
        if (brand == null) {
            return "/admin/error";
        }
        productRef.setBrandId(brand.getId());
        productRefService.createProductGroup(productRef.getName(), productRef.getDescn(), brand.getId());
        return "redirect:/sales/pr/group/list";
    }

    @RequestMapping(value = "ad/create")
    public String createProductAd(AdminProductRefForm productRef, HttpServletRequest request,
                                  HttpServletResponse response) {
        Brand brand = BrandSalesManager.getBrandByUser(brandService, request, response);
        if (brand == null) {
            return "/admin/error";
        }
        String imageUrl = null;
        if (productRef != null && productRef.getImageUrlFile() != null && productRef.getImageUrlFile().getSize() > 0) {
            imageUrl = imgUploadService.upload(productRef.getImageUrlFile(), "product_ad_logo");
        }
        productRefService.createProductAd(productRef.getName(), imageUrl, brand.getId());
        return "redirect:/sales/pr/ad/list";
    }
}
